/*
 * Button.cpp
 *
 *  Created on: 2011-1-14
 *      Author: Administrator
 */

#include "Button.h"

CButton::CButton()
	{
	iStyle = 0;
	iFontColor = KRgbBlack;
	iFocusedFontColor = KRgbBlack;
	iFocusImage = NULL;
	iLostFocusImage = NULL;
	}

CButton::~CButton()
	{
	CImageFactory::getInstance()->ReleaseImage(iFocusImage);
	CImageFactory::getInstance()->ReleaseImage(iLostFocusImage);
	}

CButton* CButton::NewL(const TRect& aRect,const CCoeControl* aParent)
	{
	CButton* self = CButton::NewLC(aRect,aParent);
    CleanupStack::Pop(self);
    return self;
	}

CButton* CButton::NewLC(const TRect& aRect,const CCoeControl* aParent)
	{
	CButton* self = new(ELeave) CButton();
    CleanupStack::PushL(self);
    self->ConstructL(aRect,aParent);
    return self;
	}

void CButton::ConstructL(const TRect& aRect,const CCoeControl* aParent)
	{
    if(aParent == NULL)
        {
        CreateWindowL();
        }
    else
        {
        SetContainerWindowL(*aParent);
        }
    SetRect(aRect);
    ActivateL();
	}

void CButton::Draw(CWindowGc& gc, const TRect& aRect) const
	{
	CBaseControl::Draw(gc,aRect);
	if (iStyle == ENormal)
		gc.DrawRect(aRect);
	if (Caption())
		{
		gc.UseFont(Font());
		if (IsFocused())
			gc.SetPenColor(iFocusedFontColor);
		else
			gc.SetPenColor(iFontColor);
		TInt baseline = aRect.Height() / 2 + Font()->AscentInPixels() / 2;
		gc.DrawText(*Caption(), aRect, baseline, CGraphicsContext::ECenter, 0);
		gc.DiscardFont();
		}
	}

void CButton::DrawFocusEffect(CWindowGc& gc, const TRect& aRect) const
	{
	if (iFocusImage)
		CGraphicsTool::DrawImage(gc, iFocusImage->Bitmap(),iFocusImage->MaskBitmap(), aRect);
	}

void CButton::DrawLostFocusEffect(CWindowGc& gc, const TRect& aRect) const
	{
	if (iLostFocusImage)
		CGraphicsTool::DrawImage(gc, iLostFocusImage->Bitmap(),iLostFocusImage->MaskBitmap(), aRect);
	}

TKeyResponse CButton::OfferKeyEventL( 
			const TKeyEvent& aKeyEvent, 
			TEventCode aType )
	{
	if (aType == EEventKey)
		{
		if (aKeyEvent.iCode == EKeyDevice3)
			{
			if (iStyle == EToolBarItem && IsFocused())
				return EKeyWasNotConsumed;
			if (iControlEventObserver)
				{
				iControlEventObserver->HandleControlEventL(this,EClick);
				return EKeyWasConsumed;
				}
			}
		}
	return EKeyWasNotConsumed;
	}

void CButton::HandlePointerEventL(const TPointerEvent& aPointerEvent)
	{
    switch (aPointerEvent.iType)
       {
           case TPointerEvent::EButton1Up:
           case TPointerEvent::EButton2Up:
           case TPointerEvent::EButton3Up:
               {
				if (iControlEventObserver)
					iControlEventObserver->HandleControlEventL(this,EClick);
               }
               break;
       }
	}

void CButton::SetLayout()
	{
	CBaseControl::SetLayout();
	TRect rect = Rect();
	TInt height = Font()->HeightInPixels();
	TInt width = 0;
	if (Caption())
		width = Font()->TextWidthInPixels(*Caption()) + 10;
	if (height < rect.Height())
		height = rect.Height();
	if (width < rect.Height())
		width = rect.Height();
	rect.SetHeight(height);
	rect.SetWidth(width);
	SetRect(rect);
	}

void CButton::setStyle(TInt aStyle)
	{
	iStyle = aStyle;
	}

void CButton::setFocusImageL(CImage* aImage)
	{
	//delete iFocusImage;
	CImageFactory::getInstance()->ReleaseImage(iFocusImage);
	iFocusImage = aImage;
	}

void CButton::setLostFocusImageL(CImage* aImage)
	{
	//delete iLostFocusImage;
	CImageFactory::getInstance()->ReleaseImage(iLostFocusImage);
	iLostFocusImage = aImage;
	}
